package com.lxg.springboot.util;


import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.TimeZone;

import static org.hibernate.validator.internal.util.CollectionHelper.newHashMap;

/**
 *
 */
public class Common {

    /**
     * 获取今天
     *
     * @return
     */
    public static String getTodayDayBeforeDate() throws ParseException {
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyyMMdd");
        String s = simpleDateFormat1.format(new Date());
        return s;
    }


    /**
     * 获取缓存得key
     *
     * @param time
     * @return
     */
    public static String getKey(Integer time) {
        long stamp = System.currentTimeMillis() + time * 1000;
        String pattern = "yyyy-MM-dd HH:mm:ss";
        if (time > 0 && time <= 60) {
            pattern = "yyyy_MM_dd_HH_mm";
        } else if (time > 60 && time <= 3600) {
            pattern = "yyyy_MM_dd_HH";
        } else if (time > 3600 && time <= 86400) {
            pattern = "yyyy_MM_dd";
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy_MM_dd_HH_mm");
        Date date = new Date(stamp);
        return simpleDateFormat.format(date);
    }


    /**
     * 获取当前得小时数目
     *
     * @return
     */
    public static String getCurrentHourDate() {
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:00:00");
        String s = simpleDateFormat1.format(new Date());
        return s;
    }

    /**
     * 获取当前时间
     *
     * @return
     */
    public static String getCurrentDate() {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(new Date());
    }

    /**
     * 获取当前时间
     *
     * @return
     */
    public static String getCurrentDateTimestamp() {
        TimeZone tz = TimeZone.getTimeZone("UTC");
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        df.setTimeZone(tz);
        return df.format(new Date());
    }

    public static Long getCurrentDateLongTime() {
        TimeZone tz = TimeZone.getTimeZone("UTC");
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        df.setTimeZone(tz);
        String format = df.format(new Date());
        Long aLong = stringToLongByForm(format, "yyyy-MM-dd'T'HH:mm:ss'Z'");
        return aLong / 1000;
    }

    /**
     * 根据时间获取时间戳
     *
     * @return
     */
    public static String getTimestampByLong(Long dateLong) {
        TimeZone tz = TimeZone.getTimeZone("UTC");
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        df.setTimeZone(tz);
        return df.format(new Date(dateLong));
    }

    public static Long getCurrentDateLong() {
        return new Date().getTime();
    }

    public static Long getCurrentSecondDateLong() {
        return new Date().getTime() / 1000;
    }

    /**
     * byte 转 String
     *
     * @param src
     * @return
     */
    public static String bytes2Hex(byte[] src) {
        if (src == null || src.length <= 0) {
            return null;
        }
        char[] res = new char[src.length * 2]; // 每个byte对应两个字符
        final char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
        for (int i = 0, j = 0; i < src.length; i++) {
            res[j++] = hexDigits[src[i] >> 4 & 0x0f]; // 先存byte的高4位
            res[j++] = hexDigits[src[i] & 0x0f]; // 再存byte的低4位
        }
        return new String(res);
    }

    /**
     * 获取今日期号
     *
     * @return
     */
    public static String getPeriod() {
        DateFormat format = new SimpleDateFormat("yyyyMMdd");
        return format.format(new Date());
    }

    public static void main(String[] args) throws ParseException {
        Long l = getCurrentSecondDateLong();
        System.out.println(l);
    }

    public static Long stringToLong(String dateString) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date dt2 = sdf.parse(dateString);
            long lTime = dt2.getTime();
            return lTime;
        } catch (Exception e) {
            return null;
        }
    }

    public static Long stringToLongByForm(String dateString, String form) {
        SimpleDateFormat sdf = new SimpleDateFormat(form);
        try {
            Date dt2 = sdf.parse(dateString);
            long lTime = dt2.getTime();
            return lTime;
        } catch (Exception e) {
            return null;
        }
    }

    public static Long stringToLong(String dateString, String formDate) {
        SimpleDateFormat sdf = new SimpleDateFormat(formDate);
        Long aLong = stringToLong(dateString);
        return aLong;
    }


    public static String isNotEmptyBatch(String... strs) {
        for (String str : strs) {
            if (str == null || "".equals(str) || str.equals("0"))
                return str + "不能为空";
        }
        return "true";
    }


    public static Map ObjectToMapUtil(Object obj) throws Exception {
        Class type = obj.getClass();
        Map returnMap = newHashMap();
        BeanInfo beanInfo = Introspector.getBeanInfo(type);
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor descriptor : propertyDescriptors) {
            String propertyName = descriptor.getName();
            if (!propertyName.equals("class")) {
                Method readMethod = descriptor.getReadMethod();
                Object result = readMethod.invoke(obj);
                if (result != null) {
                    returnMap.put(propertyName, result);
                } else {
                    returnMap.put(propertyName, "");
                }
            }
        }
        return returnMap;
    }

}
